package com.xsjl.email.mqListener;


import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Component
@RabbitListener(queues = "sms")
public class EamilCodeListener {

    @Autowired
    private JavaMailSenderImpl mailSender;
    @RabbitHandler
    public void handlerEmailCode(Map<String, String> map) {
        System.out.println("email:" + map.get("email"));
        System.out.println("code:" + map.get("code"));
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("邮箱验证");
        mailMessage.setText(map.get("email") +",您好:\n" +
                "        感谢您使用小蒜交流平台！\n" +
                "        你的验证码为" + map.get("code")+",请尽快完成注册,验证码十分钟内有效\n" +
                "                                                                                小蒜科技\n" +
                "                                                                                " + simpleDateFormat.format(date));
        mailMessage.setFrom("1923834375@qq.com");
        mailMessage.setTo(map.get("email"));
        mailSender.send(mailMessage);
    }
}
